from flask import Flask,request,session,redirect

def auth():
    if request.path.startswith('/static'):
        return
    if request.path == '/login' or request.path == '/sign':
        return
    user_info = session.get('user_info')
    if user_info:
        return
    return redirect('/login')

# 获取全局用户名
def get_account():
    user_info = session.get('user_info')
    if user_info:
        return user_info['account']
    
# 获取全局用户昵称
def get_name():
    user_info = session.get("user_info")
    if user_info:
        return user_info['remark']
    else:
        return "未登录"
    
    
# 获取全局用户id  
def get_user_id():
    user_info = session.get('user_info')
    if user_info:
        return user_info['ID']
    
# 获取全局用户权限
def get_user_role():
    user_info = session.get('user_info')
    if user_info:
        return user_info['role']

def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = '123456'

    from .views import account
    from .views import bill
    from .views import user
    from .views import view

    app.register_blueprint(account.ac)
    app.register_blueprint(bill.bi)
    app.register_blueprint(user.us)
    app.register_blueprint(view.vi)

    app.before_request(auth)
    app.template_global()(get_account)
    app.template_global()(get_name)
    app.template_global()(get_user_id)
    app.template_global()(get_user_role)
    return app